package com.skill.shareskill.activity;


import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.skill.shareskill.R;


public class StartActivity extends AppCompatActivity implements View.OnClickListener {
   /*private String id=null;
    private String name=null;
    private String phone=null;
    private String type=null;
    private String token=null;*/
   private static final int TAG_PERMISSION = 1023;
    private static final String[] SQL_COLUMN = new String[]{
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.start);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_PHONE_STATE)) {
                Toast.makeText(StartActivity.this, "deny for what???", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(StartActivity.this, "show the request popupwindow", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_PHONE_STATE},
                        TAG_PERMISSION);
            }
        } else {
            next();
        }


    }
    private void next(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.RECORD_AUDIO)) {
                Toast.makeText(StartActivity.this, "deny for what???", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(StartActivity.this, "show the request popupwindow", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.RECORD_AUDIO},
                        TAG_PERMISSION);
            }
        } else {
            next2();
        }
    }
    private void next2(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                Toast.makeText(StartActivity.this, "deny for what???", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(StartActivity.this, "show the request popupwindow", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        TAG_PERMISSION);
            }
        } else {
            logset();
        }
    }
    private void logset(){
        SharedPreferences sp = getSharedPreferences("loginName",
            Context.MODE_PRIVATE);
        String name = sp.getString("name", "");
        // 判断是否是之前有登录过
        if (name.length() == 0) {
            Intent intent=new Intent(this,LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent=new Intent(this,MainActivity.class);
            intent.putExtra("name",name);
            startActivity(intent);
            finish();
        }
        System.out.println("name="+name);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case TAG_PERMISSION: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(StartActivity.this, "allow", Toast.LENGTH_SHORT).show();
                    logset();
                } else {
                    Toast.makeText(StartActivity.this, "deny", Toast.LENGTH_SHORT).show();
                }
                return;
            }
        }
    }


    @Override
    public void onClick(View view) {

    }

}
